/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MaterialPack;

import Supporter.ImageSupporter;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Van persie
 */
public class SubGroup extends MaterialObject {
    InfoPanel BlurLabel = null;
    InfoPanel ActiveLabel = null;
    
    Image background = null;
    Image orgBackground = null;
    private Vector Items = null;
    Vector Objects = null;
    
    int orgX,orgY;
    int tempWid,tempHei;
    boolean isRepair = false;
    int Icrment = 25;
    boolean isChanged = true;
    public SubGroup(String backgPath,String name, int  x,int y, int wid,int hei )
    {
       
        orgX = xPos = x;       
        orgY = yPos = y;
        width = wid;
        height = hei;
        
        ImageSupporter imgSupp = new ImageSupporter();
        background = ImageSupporter.alphaBlend(ImageSupporter.scaleImage(imgSupp.getImage(backgPath), width, height));
        orgBackground = Image.createImage(background);
        Items = new Vector();
        Objects = new Vector();
        
        ActiveLabel = new InfoPanel("/Heading.png", name, x + width - 3 - width/3  , y - 30 , width/3, 30, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC  ,Font.SIZE_LARGE));
        ActiveLabel.SetColorText(10, 10, 50);
        ActiveLabel.SetAlphaBack();
        
        BlurLabel = new InfoPanel("/Plane.png", name, x + width - 3 - 2*width/3,  y - 30 , width/3,30, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC  ,Font.SIZE_LARGE));
        BlurLabel.SetColorText(220, 180, 150);
        BlurLabel.SetAlphaBack();
        isVisible = false;
    }
     public void Check()
             
             
             
    {
        
    }
    public boolean Press(int x,int y)
    {
        if(isVisible == false)
            return false;
        boolean ret  = false;
        for(int i = 0 ; i < Objects.size(); i++)       
        {
            
            if(((MaterialObject)Objects.elementAt(i)).Press(x,y))
            {
                
                ret = true;
            }
            
        }
        return ret;
    }
    public void Drag(int x, int y)
    {
        if(isVisible == false)
            return ;
        for(int i = 0 ; i < Objects.size(); i++)       
            ((MaterialObject)Objects.elementAt(i)).Drag(x, y);
    }
    public boolean Release(int x, int y)
    {
        if(isVisible == false)
            return false;
        for(int i = 0 ; i < Objects.size(); i++)       
            ((MaterialObject)Objects.elementAt(i)).Release(x,y);
        return false;
    }
    public void Add(MaterialObject obj)
    {
        if(obj.getClass() == PetItem.class)
        {
            ((PetItem)obj).SetATT(2, 1,0,0, 5);
            getItems().addElement(obj);
        }
        Objects.addElement(obj);
    }
    public void ArrangeItem()
    {
        int scalar = ((MaterialObject)getItems().elementAt(0)).getWidth() ;
        int wid = 15;
        int hei = 20;
        int counts = getItems().size();
        int cols = (width-wid)/(wid + scalar);
        int rows = counts/cols ;
        for(int i = 0; i  < counts; i++)     
        {
            if(getItems().elementAt(i).getClass() == PetItem.class)
            {
                ((MaterialObject)getItems().elementAt(i)).setxPos(orgX + wid*(i%cols+1) + (i%cols)*scalar );
                ((MaterialObject)getItems().elementAt(i)).setyPos(orgY + hei*(i/cols+1) + (i/cols)*scalar + 3*scalar/2 );
            }
//            ((MaterialObject)Items.elementAt(i)).setWidth(scalar);
//            ((MaterialObject)Items.elementAt(i)).setHeight(scalar);
        }
    }
    public boolean Expand(int anchor)
    {
        tempWid += Icrment;
        tempHei += Icrment;
        if(anchor == Graphics.LEFT)
        {
            setxPos(orgX + getWidth() - tempWid);
            setyPos(orgY + getHeight()/2 - tempHei/2);
        }
        else
        {
            setxPos(orgX);
            setyPos(orgY + getHeight()/2 - tempHei/2);
        }
        
        
        if( tempWid >= getWidth() && tempHei >= getHeight())
        {
            
            setxPos(orgX);
            setyPos(orgY);
            setIsVisible(true);
            background = Image.createImage(orgBackground);
            isChanged = true;
            return true;
        }
        background = ImageSupporter.scaleImage(orgBackground, tempWid, tempHei);
        return false;
    }
    public boolean Shrink(int anchor)
    {           
        tempWid -= Icrment;
        tempHei -= Icrment;
        if(anchor == Graphics.RIGHT)
        {
            setxPos(orgX + getWidth() - tempWid);
            setyPos(orgY + getHeight()/2 - tempHei/2);
        }
        else
        {
            setxPos(orgX);
            //xPos = orgX + width - tempWid;
            setyPos(orgY + getHeight()/2 - tempHei/2);
        }
        
        if( tempWid <= 0 && tempHei <= 0)
        {
            setxPos(orgX);
            setyPos(orgY);
            background = Image.createImage(orgBackground);

            setIsVisible(false);
            isChanged = true;
            return true;
        }
        background = ImageSupporter.scaleImage(orgBackground, tempWid, tempHei);
        return false;
    }
    public void setIsVisible(boolean  bit)
    {
        super.setIsVisible(bit);
        for(int i = 0 ; i < Objects.size(); i++)       
            ((MaterialObject)Objects.elementAt(i)).setIsVisible(bit);
    }
    
    public void Back()
    {
        //setIsVisible(false);
        isVisible = true;
        
        tempWid = getWidth();            
        tempHei = getHeight();
        isChanged = false;
    }
    public void Front()
    {
        //setIsVisible(false);
        isVisible = true;      
        
        tempWid = 0;
        tempHei = 0;
        isChanged = false;
    }
    public void paint(Graphics g)
    {
        if(isVisible == true)
        {
            
            g.drawImage(background, getxPos(), getyPos(),Graphics.TOP | Graphics.LEFT);
            
            if(isChanged)
            {
                  for(int i=0; i <Objects.size(); i++)
                    ((MaterialObject)Objects.elementAt(i)).paint(g);
                ActiveLabel.paint(g);
            }
        }   
        else 
            BlurLabel.paint(g);
    }

    /**
     * @return the Items
     */
    public Vector getItems() {
        return Items;
    }

    /**
     * @param Items the Items to set
     */
    public void setItems(Vector Items) {
        this.Items = Items;
    }
}
